package utility;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Utility {

	
	private Statement sentencia;

	public Utility(FileSystemXmlApplicationContext fAC) throws SQLException{
		BasicDataSource bDT = (BasicDataSource) fAC.getBean("myDataSource");

		Connection conexion;
		conexion = bDT.getConnection();

		sentencia = conexion.createStatement();
	}
	
	

	public static boolean createDir(String dir) {
		File directorio = new File(dir);
		return directorio.mkdirs();
	}

	public static void createFile(File f, String content) throws IOException {

		FileOutputStream fOS;
		fOS = new FileOutputStream(f);
		fOS.write(content.getBytes());
		fOS.flush();
		fOS.close();
	}

	
	public static void deleteDir(File dir) {
		File[] files = dir.listFiles();
		if (files != null)
			for (int x = 0; x < files.length; x++) {
				if (files[x].isDirectory())
					deleteDir(files[x]);
				else
					files[x].delete();
			}
		dir.delete();

	}

	
	public void dropDataBase()throws SQLException {
		sentencia.execute("DROP SCHEMA public CASCADE");

	}
	
	public void recordsDelete(String table)throws SQLException {
		sentencia.execute("delete from public."+table);

	}
	
	public void deleteTable(String table){
		try {
			sentencia.execute("drop table public."+table);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		

	}

}
